package com.asia5b.a5mall.middleware.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import com.asia5b.a5bmall.resource.ResString;
import com.asia5b.a5mall.middleware.R;


/**
 * Created by zhangyuncai on 2018/9/12.
 */
public class HeadBar extends RelativeLayout {

    private TextView tv_title_back;
    public TextView tv_title_titletxt;
    private View view_title_bottomline;
    private StatusHeightView status_title;
    private LinearLayout ll_head_gen;
    public TextView tv_title_righttxt;

    //左边文字
    private String mleft;
    //中间文字
    private String mTitle;
    //邮编文字
    private String mRight;
    //返回按钮是否隐藏
    private boolean isBackIconGone;
    //是否隐藏底部分割线
    private boolean isBottomLineGone;
    //是否隐藏标题栏高度
    private boolean isStatusGone;
    //返回按钮icon
    private int leftImgRes;
    //右边icon
    private int rightImgRes = -1;
    //右边文字大小
    private int rightTextSize;
    //左边文字大小
    private int leftTextSize;
    //右边文字颜色
    private int rightTextColor;
    //左边文字颜色
    private int leftTextColor;
    //中间文字颜色
    private int centerTextColor;
    //背景颜色
    private int backColor;
    //状态栏颜色
    private int statusColor;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public HeadBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HeadBar);
        mTitle = ta.getString(R.styleable.HeadBar_titleText);
        mleft = ta.getString(R.styleable.HeadBar_leftText);
        mRight = ta.getString(R.styleable.HeadBar_rightText);
        isBackIconGone = ta.getBoolean(R.styleable.HeadBar_isBackIconGone, false);
        isBottomLineGone = ta.getBoolean(R.styleable.HeadBar_isBottomLineGone, false);
        isStatusGone = ta.getBoolean(R.styleable.HeadBar_isStatusGone, false);
        leftImgRes = ta.getResourceId(R.styleable.HeadBar_leftImgRes, R.drawable.ic_back2_black);
        rightImgRes = ta.getResourceId(R.styleable.HeadBar_rightImgRes, -1);

        rightTextSize = ta.getDimensionPixelSize(R.styleable.HeadBar_rightTextSize, sp2px(getContext(), 14));
        leftTextSize = ta.getDimensionPixelSize(R.styleable.HeadBar_leftTextSize, sp2px(getContext(), 14));
        rightTextColor = ta.getColor(R.styleable.HeadBar_rightTextColor, ContextCompat.getColor(getContext(), R.color.common_text_black));
        leftTextColor = ta.getColor(R.styleable.HeadBar_leftTextColor, ContextCompat.getColor(getContext(), R.color.common_text_black));
        centerTextColor = ta.getColor(R.styleable.HeadBar_centerTextColor, ContextCompat.getColor(getContext(), R.color.common_text_black));
        backColor = ta.getColor(R.styleable.HeadBar_backColor, ContextCompat.getColor(getContext(), R.color.white));
        statusColor = ta.getColor(R.styleable.HeadBar_statusColor, ContextCompat.getColor(getContext(), R.color.transparency));
        ta.recycle();

        tv_title_titletxt.setTextColor(centerTextColor);
        tv_title_back.setTextColor(leftTextColor);
        tv_title_back.setTextSize(px2sp(getContext(), leftTextSize));
        tv_title_righttxt.setTextColor(rightTextColor);
        tv_title_righttxt.setTextSize(px2sp(getContext(), rightTextSize));
        ll_head_gen.setBackgroundColor(backColor);
        status_title.setBackgroundColor(statusColor);
        setBackIconGone(isBackIconGone);
        if (isBottomLineGone) {
            view_title_bottomline.setVisibility(GONE);
        }
        setStatusGone(isStatusGone);
        if (!TextUtils.isEmpty(mleft)) {
            tv_title_back.setText(mleft);
        }
        if (!TextUtils.isEmpty(mTitle)) {
            setTitleName(mTitle);
        }
        if (!TextUtils.isEmpty(mRight)) {
            tv_title_righttxt.setText(mRight);
            tv_title_righttxt.setVisibility(VISIBLE);
        } else {
            tv_title_righttxt.setText("");
            tv_title_righttxt.setVisibility(GONE);
        }
        if (leftImgRes != -1) {
            tv_title_back.setCompoundDrawablesWithIntrinsicBounds(leftImgRes, 0, 0, 0);
        }
        if (rightImgRes != -1) {
            tv_title_righttxt.setVisibility(VISIBLE);
            tv_title_righttxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, rightImgRes, 0);
        }

    }

    public HeadBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadBar(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.head_bar, this, true);
        tv_title_back = findViewById(R.id.tv_title_back);
        tv_title_righttxt = findViewById(R.id.tv_title_righttxt);
        tv_title_titletxt = findViewById(R.id.tv_title_titletxt);
        view_title_bottomline = findViewById(R.id.view_title_bottomline);
        status_title = findViewById(R.id.status_title);
        ll_head_gen = findViewById(R.id.ll_head_gen);
    }


    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param pxValue
     * @param pxValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue
     * @param spValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    private int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public void setBackIconGone(boolean isBackIconGone) {
        this.isBackIconGone = isBackIconGone;
        if (isBackIconGone) {
            tv_title_back.setVisibility(INVISIBLE);
            tv_title_back.setEnabled(false);
        } else {
            tv_title_back.setVisibility(VISIBLE);
            tv_title_back.setEnabled(true);
        }
    }

    public void setStatusGone(boolean isStatusGone) {
        this.isStatusGone = isStatusGone;
        if (isStatusGone) {
            status_title.setVisibility(GONE);
        } else {
            status_title.setVisibility(VISIBLE);
        }
    }

    public void setRithtText(@StringRes int mRight) {
        this.mRight = ResString.get(mRight);
        setRithtText(this.mRight);
    }

    public void setRithtText(String mRight) {
        this.mRight = mRight;
        if (!TextUtils.isEmpty(mRight)) {
            tv_title_righttxt.setText(mRight);
            tv_title_righttxt.setVisibility(VISIBLE);
        } else {
            tv_title_righttxt.setText("");
            tv_title_righttxt.setVisibility(GONE);
        }
    }

    public void setRightClickListener(View.OnClickListener listener) {
        tv_title_righttxt.setOnClickListener(listener);
    }

    public void setTitleName(String titleName) {
        this.mTitle = titleName;
        tv_title_titletxt.setText(titleName);
    }

    public void setTitleName(@StringRes int titleName) {
        this.mTitle = ResString.get(titleName);
        tv_title_titletxt.setText(titleName);
    }

}
